var P2 = function (game) {

    this.create = function () {
        ClosePage(game, '2_0', '2', '2_r', '2_w');

        function Page(game, cbg, mbg, rbg, wbg) {

            var bimgs = ['d2_0', 'd2_1', 'd2_2', 'd2_3', 'd2_4', 'd2_5', 'd2_6', 'd2_7'];
            dragPos = [
                [770, 160], [900, 160], [1030, 160], [1160, 160], [770, 320], [900, 320], [1030, 320], [1160, 320]
            ];
            const cup = ['拿铁', '美式咖啡', '醇艺白咖啡', '卡布奇诺', '可塔朵', '意式浓缩', '摩卡', '瑞巧多'];
            const rightCup = ['拿铁', '意式浓缩', '卡布奇诺', '可塔朵', '醇艺白咖啡'];

            var flagAry = [2, 2, 2, 2];
            var container = game.add.sprite(0, 0, '');

            game.add.sprite(0, 0, mbg);

            var img;
            var group = game.add.group();
            for (var i = 0; i < 8; i++) {
                img = game.add.sprite(dragPos[i][0], dragPos[i][1], bimgs[i]);
                img.name = cup[i];
                img.data = i;
                group.addAt(img, i);
                img.inputEnabled = true;
                img.input.useHandCursor = true;
                img.input.enableDrag(false, true);
                img.events.onDragStop.add(fixLocation);
            }


            var bag = game.add.sprite(600, 480, 'small');
            game.world.bringToTop(group);


            var cupSprite, text;
            var cupWord = [];
            var cupGroup = game.add.group();

            var style = {
                font: "9px",
                fill: "#1666A5",
                align: "center",
                wordWrap: true,
                wordWrapWidth: 20
            };

            function isSelect(word, ary) {
                return ary.some((item) => {
                    return item === word;
                })
            }

            function showCup() {
                if (cupGroup.children) {
                    cupGroup.destroy();
                }
                cupGroup = game.add.group();
                for (var i = 0; i < cupWord.length; i++) {
                    cupSprite = game.add.sprite(1030 + i * 50, 510, '');

                    cupSprite.addChild(game.make.sprite(0, 0, 'glass'));
                    text = game.make.text(0, 55, cupWord[i], style);
                    text.x = (37 - text.width) / 2;
                    cupSprite.addChild(text);

                    cupSprite.data = cupWord[i];
                    cupSprite.name = cupWord[i];
                    cupSprite.inputEnabled = true;
                    cupSprite.input.useHandCursor = true;
                    cupSprite.input.useHandCursor = true;
                    cupSprite.events.onInputDown.add(cupToOrigin, this);
                    cupGroup.add(cupSprite);
                }
            }


            function cupToOrigin(arg) {
                cupWord = cupWord.filter(function (item, index) {
                    return arg.data != item;
                });
                showCup();
                group.getByName(arg.name).scale.setTo(1, 1);
            }


            function fixLocation(item) {

                var bounds = item.getBounds();
                var boundsB = bag.getBounds();

                var flag = Phaser.Rectangle.intersects(bounds, boundsB);
                if (flag && (!isSelect(item.name, cupWord))) {
                    cupWord.push(item.name);
                    showCup();
                    item.scale.setTo(0.0, 0.0)

                }

                tween = game.add.tween(item).to({
                    x: dragPos[item.data][0],
                    y: dragPos[item.data][1]
                }, 300, Phaser.Easing.Linear.None, true);
            }

            game.add.button(1100, 630, imgs['prev'], onPrev, this, 2, 1, 0);
            game.add.button(1250, 630, imgs['submit'], onSubmit, this, 2, 1, 0);

            function onPrev() {
                game.goState('back');
                group.destroy();
            }

            function onSubmit(arg) {
                var flag = true;
                rightCup.forEach(function (item) {
                    var ans = cupWord.some(function (val) {
                        return item === val;
                    })
                    if (ans === false) {
                        flag = false;
                        return;
                    }
                })
                container.destroy();
                if (flag) {
                    RightPage(game, cbg, mbg, rbg, wbg);
                } else {
                    WrongPage(game, cbg, mbg, rbg, wbg);
                }
            }
        }

        function ClosePage(game, cbg, mbg, rbg, wbg) {
            var container = game.add.sprite(0, 0, '');
            var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
            container.addChild(close);

            function onClose() {
                container.destroy();
                Page(game, cbg, mbg, rbg, wbg);
            }
        }

        function WrongPage(game, cbg, mbg, rbg, wbg) {
            var container = game.add.sprite(0, 0, '');

            var bg = game.add.image(0, 0, wbg);
            container.addChild(bg);

            var next = game.add.button(1250, 630, imgs['retry'], onClick, this, 2, 1, 0);
            container.addChild(next);
            function onClick() {
                container.destroy();
                Page(game, cbg, mbg, rbg, wbg);
            }
        }

        function RightPage(game, cbg, mbg, rbg, wbg) {
            var container = game.add.sprite(0, 0, '');
            var bg = game.add.image(0, 0, rbg);
            container.addChild(bg);
            var next = game.add.button(1250, 630, imgs['next'], onClick, game, 2, 1, 0);
            container.addChild(next);

            function onClick() {
                game.goState('next');
            }
        }
    }

}





